<?php
/**
 * ActionFilter.php
 * Created by HappyOnion.
 * User: yang
 * Date: 2018/1/2 Time: 10:00
 */

namespace backend\controllers;

use backend\models\AuthAssignment;
use common\models\Package;
use common\models\Project;
use common\models\ProjectTeam;
use common\models\PackageInitiation;
use webvimark\modules\UserManagement\models\rbacDB\Route;
use webvimark\modules\UserManagement\models\User;
use yii\base\Action;
use Yii;
use yii\base\ActionFilter;
use yii\web\ForbiddenHttpException;
use yii\db\Expression;

class DtActionFilter extends ActionFilter
{
    private $_startTime;
    public function beforeAction($action)
    {

        $packageid  = intval(\Yii::$app->request->get('packageid',0));
        $project_id = intval(\Yii::$app->request->get('project_id',0));

        if(!$this->checkPackage($action->uniqueId,$packageid) || !$this->checkProject($action->uniqueId,$project_id)){
            if (Yii::$app->request->isAjax) {
                $response = Yii::$app->response;
                $response->format = \yii\web\Response::FORMAT_JSON;
                $response->data = ['status' => 'false','msg'=>Yii::t('yii', 'You are not allowed to perform this action.')];
                $response->send();
                exit;
            }else{
                throw new ForbiddenHttpException(Yii::t('yii', 'You are not allowed to perform this action.'));
            }
        }


        $this->_startTime=microtime(true);
        Yii::trace("DtAction '{$action->uniqueId}' is begin  .");
        return parent::beforeAction($action);
    }

    public function afterAction($action,$result){
        $time=microtime(true)-$this->_startTime;
        Yii::trace("DtAction '{$action->uniqueId}' spent $time second.");
        return parent::afterAction($action,$result);
    }


    static public  function checkProject($action,$id){

        $admin_auth = AuthAssignment::find()->where(['user_id'=>Yii::$app->user->id])->asArray()->one();
        if($admin_auth['item_name']=='Admin'){
            return true;
        }

        if ( Yii::$app->user->isSuperadmin || !$id)
        {
            return true;
        }

        $row_project_info     = Project::find()->where(['id'=>$id])->asArray()->one();

        $row_package_info = Package::find()->where(['id'=>$row_project_info['package_id']])->asArray()->one();
        $package_id = $row_package_info['id'];
        $row_package_owner = Package::find()->where(new Expression('FIND_IN_SET(:owner, owner)'))->addParams([':owner' => Yii::$app->user->id])->andWhere(['id'=>$package_id])->asArray()->one();
        $row_project_kfw     = Project::find()->where(new Expression('FIND_IN_SET(:access_users, access_users)'))->addParams([':access_users' => Yii::$app->user->id])->andWhere(['id'=>$id])->asArray()->one();
        $row_project_manager = ProjectTeam::find()->where(new Expression('FIND_IN_SET(:manager, manager)'))->addParams([':manager' => Yii::$app->user->id])->andWhere(['project_id'=>$id])->asArray()->one();
        $row_project_owner = ProjectTeam::find()->where(new Expression('FIND_IN_SET(:owner, owner)'))->addParams([':owner' => Yii::$app->user->id])->andWhere(['project_id'=>$id])->asArray()->one();
        $row_project_partner = ProjectTeam::find()->where(new Expression('FIND_IN_SET(:partner, partner)'))->addParams([':partner' => Yii::$app->user->id])->andWhere(['project_id'=>$id])->asArray()->one();

        $row_package_zrr = PackageInitiation::find()->where(new Expression('FIND_IN_SET(:leader, leader)'))->addParams([':leader' => Yii::$app->user->id])->andWhere(['package_id'=>$package_id])->asArray()->one();
        $row_package_xt1 = PackageInitiation::find()->where(new Expression('FIND_IN_SET(:due_diligence, due_diligence)'))->addParams([':due_diligence' => Yii::$app->user->id])->andWhere(['package_id'=>$package_id]) ->addParams([':due_diligence' => Yii::$app->user->id])->asArray()->one();
        $row_package_xt2 = PackageInitiation::find()->where(new Expression('FIND_IN_SET(:pre_evaluation, pre_evaluation)'))->addParams([':pre_evaluation' => Yii::$app->user->id])->andWhere(['package_id'=>$package_id]) ->addParams([':pre_evaluation' => Yii::$app->user->id])->asArray()->one();
        $row_package_xt3 = PackageInitiation::find()->where(new Expression('FIND_IN_SET(:zongjian, zongjian)'))->addParams([':zongjian' => Yii::$app->user->id])->andWhere(['package_id'=>$package_id]) ->addParams([':zongjian' => Yii::$app->user->id])->asArray()->one();

        $row_project_manager_have = ProjectTeam::find()->andWhere(['project_id'=>$id])->asArray()->one();

        $row_auth_user = AuthAssignment::find()->where(['item_name'=>'Companyleader','user_id'=>Yii::$app->user->id])->asArray()->one();


        switch($action){
            case 'project_permissions':
            case 'project/set-permissions':        //单项目设置权限
                if($row_project_manager['project_id'] or  $row_project_owner['project_id'])
                    return true;
                else
                    if($row_project_manager_have['project_id']){
                            return false;
                    }else{
                        if($row_project_owner['project_id'] or $row_package_zrr['package_id'] or $row_package_xt1['package_id'] or $row_package_xt2['package_id'] or $row_package_xt3['package_id'] or $row_project_kfw['project_id']){
                            return true;
                        }else{
                            return false;
                        }
                    }
                break;
            case 'project_team':
            case 'project/ajax-save-team-project': //配置团队
                if($row_project_manager['project_id'] or  $row_project_owner['project_id'] )
                    return true;
                else
                    if($row_project_manager_have['project_id']){
                        return false;
                    }else {
                        if ($row_project_owner['project_id'] or $row_package_zrr['package_id'] or $row_package_xt1['package_id'] or $row_package_xt2['package_id'] or $row_package_xt3['package_id']) {
                            return true;
                        } else {
                            return false;
                        }
                    }
                break;
                break;
            case 'project_light':
            case 'project/set-project-light': //亮点项目
                if($row_project_manager['project_id'] or $row_project_owner['project_id'] or $row_project_partner['project_id']  )
                    return true;
                else
                    if($row_project_manager_have['project_id']){
                        return false;
                    }else {
                        if ($row_package_zrr['package_id'] or $row_package_xt1['package_id'] or $row_package_xt2['package_id'] or $row_package_xt3['package_id']) {
                            return true;
                        } else {
                            return false;
                        }
                    }
                break;
                break;
            case 'project_like':
            case 'package/set-package-like': //单项目包关注
                return true;
                break;
            case 'project_down':
            case 'project/down-load-report': //单项目下载
                return true;
                break;
            case 'project_view':

				if($row_project_manager_have['project_id']){
					if($row_project_info['access']==0){
                            return true;
                        break;
                    }else if($row_project_info['access']==2){
                        if($row_project_manager['project_id'] or $row_project_kfw['id']  or $row_project_owner['project_id'] or $row_project_partner['project_id'] )
                            return true;
                        else
                            return false;

                        break;
					}
				}else{
				 if($row_package_zrr['package_id']){
                    if($row_project_info['access']==0){
                            return true;
                        break;
                    }else if($row_project_info['access']==1){
                        if($row_project_manager['project_id'] or $row_package_zrr['package_id'] or $row_project_kfw['id'] or $row_project_manager['project_id'] or $row_project_owner['project_id'] or $row_project_partner['project_id'] )
                            return true;
                        else
                            if($row_package_zrr['package_id'] or $row_package_xt1['package_id'] or $row_package_xt2['package_id'] or $row_package_xt3['package_id'] or $row_project_manager['project_id'] or $row_project_owner['project_id'] or $row_project_partner['project_id'] ){
                                return true;
                            }else{
                                if($row_auth_user['user_id'])
                                    return true;
                                else
                                    return false;
                            }
                        break;
                    }else{
                        if($row_project_manager['project_id'] or $row_package_zrr['package_id'] or $row_project_kfw['id'] or $row_project_manager['project_id'] or $row_project_owner['project_id'] or $row_project_partner['project_id'] )
                            return true;
                        else
                            if($row_package_zrr['package_id'] or $row_package_xt1['package_id'] or $row_package_xt2['package_id'] or $row_package_xt3['package_id']){
                                return true;
                            }else{
                                if($row_auth_user['user_id'])
                                    return true;
                                else
                                    return false;
                            }
                        break;
                    }

                }else{
                    if($row_project_info['access']==0){
                            return true;
                        break;
                    }else if($row_project_info['access']==1){
                        if($row_project_manager['project_id'] or $row_package_zrr['id'] or $row_project_kfw['id']  or $row_project_owner['project_id'] or $row_project_partner['project_id'] )
                            return true;
                        else
                            if($row_package_zrr['package_id'] or $row_package_xt1['package_id'] or $row_package_xt2['package_id'] or $row_package_xt3['package_id']){
                                return true;
                            }else{
                                if($row_auth_user['user_id'])
                                    return true;
                                else
                                    return false;
                            }
                        break;
                    }else{
                        if($row_project_manager['project_id'] or $row_project_kfw['id']  or $row_project_owner['project_id'] or $row_project_partner['project_id'] )
                            return true;
                        else
                            if($row_package_zrr['package_id'] or $row_package_xt1['package_id'] or $row_package_xt2['package_id'] or $row_package_xt3['package_id']){
                                return true;
                            }else{
                                if($row_auth_user['user_id'])
                                    return true;
                                else
                                    return false;
                            }
                        break;
                    }

                }
				}
               
				break;
            case 'project_edit':
            case 'project/ajax-save-base-project'://单项目基本编辑
            case 'project/ajax-save-lawsuit-project'://单项目流转编辑
            case 'project/ajax-save-transfer-project'://单项目流转历史编辑
            case 'project/ajax-save-transger-history-project'://资产包流转历史编辑
            case 'project/ajax-save-evaluation-project'://资产包流转历史编辑

   			    if($row_project_manager_have['project_id']){
					if($row_project_owner['project_id']  or $row_project_partner['project_id'] or $row_project_manager['project_id'] )
						return true;
					else
						return false;
				}else{
					if($row_project_owner['project_id']  or $row_project_partner['project_id'] or $row_project_manager['project_id'] )
                    return true;
                else
                    if($row_project_manager['project_id'] or $row_package_zrr['package_id']){
                        return true;
                    }else{
                        if($row_package_owner['id'] or $row_package_xt1['package_id'] or $row_package_xt2['package_id'] or $row_package_xt3['package_id'])
                            return true;
                        else
                            return false;
                    }
				}
                
                break;
            default:
                return true;
        }

    }


    static public function checkPackage($action,$id)
    {
        if(!$id){
            $id = Yii::$app->session->get('packageid');
        }

        $admin_auth = AuthAssignment::find()->where(['user_id'=>Yii::$app->user->id])->asArray()->one();
        if($admin_auth['item_name']=='Admin'){
            return true;
        }

        if ( Yii::$app->user->isSuperadmin || !$id )
        {
            return true;
        }
        $row_package_info = Package::find()->where(['id'=>$id])->asArray()->one();
        $row_package_zongjian = PackageInitiation::find()->where(new Expression('FIND_IN_SET(:zongjian,zongjian)'))->addParams([':zongjian' => Yii::$app->user->id])->andWhere(['package_id'=>$id])->asArray()->one();
        $row_package_kfw = Package::find()->where(new Expression('FIND_IN_SET(:access_users,access_users)'))->addParams([':access_users' => Yii::$app->user->id])->andWhere(['id'=>$id])->asArray()->one();
        $row_package_owner = Package::find()->where(new Expression('FIND_IN_SET(:owner, owner)'))->addParams([':owner' => Yii::$app->user->id])->andWhere(['id'=>$id])->asArray()->one();
        $row_package_zrr_have = PackageInitiation::find()->andWhere(['package_id'=>$id])->asArray()->one();
        $row_package_zrr = PackageInitiation::find()->where(new Expression('FIND_IN_SET(:leader, leader)'))->addParams([':leader' => Yii::$app->user->id])->andWhere(['package_id'=>$id])->asArray()->one();
        $row_package_xt1 = PackageInitiation::find()->where(new Expression('FIND_IN_SET(:due_diligence, due_diligence)'))->addParams([':due_diligence' => Yii::$app->user->id])->andWhere(['package_id'=>$id]) ->addParams([':due_diligence' => Yii::$app->user->id])->asArray()->one();
        $row_package_xt2 = PackageInitiation::find()->where(new Expression('FIND_IN_SET(:pre_evaluation, pre_evaluation)'))->addParams([':pre_evaluation' => Yii::$app->user->id])->andWhere(['package_id'=>$id]) ->addParams([':pre_evaluation' => Yii::$app->user->id])->asArray()->one();
        $row_package_xt3 = PackageInitiation::find()->where(new Expression('FIND_IN_SET(:zongjian, zongjian)'))->addParams([':zongjian' => Yii::$app->user->id])->andWhere(['package_id'=>$id]) ->addParams([':zongjian' => Yii::$app->user->id])->asArray()->one();
        $row_auth_user = AuthAssignment::find()->where(['item_name'=>'Companyleader','user_id'=>Yii::$app->user->id])->asArray()->one();
        switch($action){
            case 'package_permissions':
            case 'package/set-permissions':        //资产包设置权限
                if($row_package_zongjian['package_id']){
                    return true;
                }
                if($row_package_info['access']==1 ){
                    if($row_package_owner['id'] or $row_package_zrr['package_id']){
                        return true;
                    }
                    return false;
                }
                if( $row_package_zrr_have['leader'] ){
                    if( $row_package_zrr['package_id'] )
                        return true;
                    else
                        return false;
                    break;
                }else{
                    if(  $row_package_owner['id'] )
                        return true;
                    else
                        return false;
                    break;
                }
            case 'package_initiation_plan':
            case 'package-initiation-plan/get-package-plan1': //资产包发起协同
                if($row_package_zongjian['package_id']){
                    return true;
                }
                if($row_package_info['access']==1 ){
                    if($row_package_owner['id'] or $row_package_zrr['package_id']){
                        return true;
                    }
                    return false;
                }
                if( $row_package_zrr_have['leader'] ){
                    if( $row_package_zrr['package_id'] )
                        return true;
                    else
                        return false;
                    break;
                }else{
                    if(  $row_package_owner['id']  )
                        return true;
                    else
                        return false;
                    break;
                }
                break;
            case 'package_apply':
            case 'package-apply/judge-package': //资产包申请立项
                if($row_package_zongjian['package_id']){
                    return true;
                }
                if($row_package_info['access']==1 ){
                    if($row_package_owner['id'] or $row_package_zrr['package_id']){
                        return true;
                    }
                    return false;
                }
                if( $row_package_zrr_have['leader'] ){
                    if( $row_package_zrr['package_id'] )
                        return true;
                    else
                        return false;
                    break;
                }else{
                    if(  $row_package_owner['id']  )
                        return true;
                    else
                        return false;
                    break;
                }
                break;
                break;
            case 'package_package_like':
            case 'package/set-package-like': //资产包关注
                return true;
                break;
            case 'package_down':
            case 'package/show-down-page': //资产包下载
            case 'package/down-projects': //资产包下载
                return true;
                break;
            case 'package_view':
                if($row_package_zongjian['package_id']){
                    return true;
                }
                if($row_package_info['access']==1 ){
                    if($row_package_owner['id'] or $row_package_zrr['package_id']){
                        return true;
                    }
                    return false;
                }
                if($row_package_info['access']==0 or $row_package_kfw['id'] or $row_package_owner['id'] or $row_package_zrr['package_id'] or $row_package_xt3['package_id'] or $row_package_xt2['package_id'] or $row_package_xt1['package_id']  )
                    return true;
                else
                    if($row_auth_user['user_id'])
                        return true;
                    else
                        return false;
                break;
            case 'package_edit':
            case 'package/eidt-base'://资产包基本编辑
            case 'package/eidt-transfer'://资产包流转编辑
                //case 'package/detail-info'://资产包流转历史编辑
            case 'package/edit-history-info'://资产包流转历史编辑
            case 'package/update-transfer-history'://资产包流转历史编辑
            case 'package/add-project'://资产包添加项目
            case 'package/del-project-list'://资产包删除项目
                if($row_package_zongjian['package_id']){
                    return true;
                }
                if($row_package_info['access']==1){
                    if($row_package_owner['id'] or $row_package_zrr['package_id']){
                        return true;
                    }
                    return false;
                }
                if( $row_package_zrr_have['leader'] ){
                        if( $row_package_zrr['package_id'] or $row_package_xt1['package_id'] or $row_package_xt2['package_id'] or $row_package_xt3['package_id'])
                            return true;
                        else
                            return false;
                        break;
                }else{
                    if(  $row_package_owner['id'] or $row_package_xt1['package_id'] or $row_package_xt2['package_id'] or $row_package_xt3['package_id'])
                        return true;
                    else
                        return false;
                    break;
                }
            default:
                return true;
        }


    }
}